/*
* 等值线分析对象，分层设色运用
* 可见高程范围
* 等值距
* 线条颜色
* 填充模式
*/

class isolineAnalysis{
  constructor(viewer) {
    this.viewer = viewer
    this.hyp = new Cesium.HypsometricSetting();
    //默认参数配置
    this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.LINE
    this.hyp.ColorTableMinKey = 2736.88110351563
    this.hyp.ColorTableMaxKey = 5597.06640625
    this.currentValue = ''
    //建立颜色表
    let colorTable = new Cesium.ColorTable()
    colorTable.insert(5597.06640625,new Cesium.Color(0, 0, 255/255))
    colorTable.insert(5406.3873860677086,new Cesium.Color(0, 51/255, 255/255))
    colorTable.insert(5215.7083658854172,new Cesium.Color(0, 102/255, 255/255))
    colorTable.insert(5025.0293457031257,new Cesium.Color(0, 153/255, 255/255))
    colorTable.insert(4834.3503255208343,new Cesium.Color(0, 204/255, 255/255))
    colorTable.insert(4643.6713053385429,new Cesium.Color(0, 255/255, 255/255))
    colorTable.insert(4452.9922851562524,new Cesium.Color(51/255, 255/255, 204/255))
    colorTable.insert(4262.3132649739609,new Cesium.Color(102/255, 255/255, 153/255))
    colorTable.insert(4071.6342447916695,new Cesium.Color(153/255, 255/255, 102/255))
    colorTable.insert(3880.9552246093781,new Cesium.Color(204/255, 255/255, 51/255))
    colorTable.insert(3690.2762044270867,new Cesium.Color(255/255, 255/255, 0))
    colorTable.insert(3499.5971842447952,new Cesium.Color(255/255, 204/255, 0))
    colorTable.insert(3308.9181640625038,new Cesium.Color(255/255, 153/255, 0))
    colorTable.insert(3118.2391438802129,new Cesium.Color(255/255, 102/255, 0))
    colorTable.insert(2927.5601236979214,new Cesium.Color(255/255, 51/255, 0))
    colorTable.insert(2736.88110351563,new Cesium.Color(255/255, 0, 0))
    this.hyp.ColorTable = colorTable
    this.hyp.Opacity = 0.3
    this.fillPolygon = new Cesium.DrawHandler(this.viewer,Cesium.DrawMode.Polygon,0)
    this.fillPolygon.activeEvt.addEventListener((isActive)=>{
      if(isActive == true){
        this.viewer.enableCursorStyle = false;
        this.viewer._element.style.cursor = '';
        if(document.getElementById('scene').classList.contains('drawCur')){
          document.getElementById('scene').classList.remove('drawCur')
        }
        document.getElementById('scene').classList.add('drawCur')
      }
      else{
        this.viewer.enableCursorStyle = true;
        document.getElementById('scene').classList.add('drawCur')
      }
    });
    this.fillPolygon.drawEvt.addEventListener((result)=>{
      let array = [].concat(result.object.positions);
      let positions = [];
      for(let i = 0, len = array.length; i < len; i ++){
        let cartographic = Cesium.Cartographic.fromCartesian(array[i]);
        let longitude = Cesium.Math.toDegrees(cartographic.longitude);
        let latitude = Cesium.Math.toDegrees(cartographic.latitude);
        let h=cartographic.height;
        if(positions.indexOf(longitude)==-1&&positions.indexOf(latitude)==-1){
          positions.push(longitude);
          positions.push(latitude);
          positions.push(h);
        }
      }
      //分析区域为指定区域
      this.hyp.CoverageArea = positions;
      switch (this.currentValue){
        case 'None' : this.viewer.scene.globe.HypsometricSetting = undefined;break;
        case 'Line' : {
          this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.LINE;
          this.viewer.scene.globe.HypsometricSetting = {
            hypsometricSetting : this.hyp,
            analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
          };
        }
          break;
        case 'Region' : {
          this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.FACE;
          this.viewer.scene.globe.HypsometricSetting = {
            hypsometricSetting : this.hyp,
            analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
          };
        }
          break;
        case 'Line_Region' : {
          this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.FACE_AND_LINE;
          this.viewer.scene.globe.HypsometricSetting = {
            hypsometricSetting : this.hyp,
            analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
          };
        }
          break;
        default : break;
      }
      this.fillPolygon.polygon.show = false;
      this.fillPolygon.polyline.show = false;
    });
  }

  remove(){
    this.viewer.scene.globe.HypsometricSetting = undefined
    this.hyp && (this.hyp.MaxVisibleValue = -1000)
    this.hyp && (this.hyp.MinVisibleValue = -1000)
  }

  analysis(currentValue,lineColor,lineInterval,maxVisibleValue,minVisibleValue){
    this.currentValue = currentValue
    this.hyp._lineColor =  Cesium.Color.fromCssColorString(lineColor)
    this.hyp.LineInterval = lineInterval
    this.hyp.MaxVisibleValue = maxVisibleValue
    this.hyp.MinVisibleValue = minVisibleValue
    this.fillPolygon.activate()
  }

  //填充模式
  changeFillOption(currentValue){
    switch (currentValue){
      case 'None' : viewer.scene.globe.HypsometricSetting = undefined;break
      case 'Line' : {
        this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.LINE
        this.viewer.scene.globe.HypsometricSetting = {
          hypsometricSetting : this.hyp,
          analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
        }
      }
        break;
      case 'Region' : {
        this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.FACE;
        this.viewer.scene.globe.HypsometricSetting = {
          hypsometricSetting : this.hyp,
          analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
        }
      }
        break;
      case 'Line_Region' : {
        this.hyp.DisplayMode = Cesium.HypsometricSettingEnum.DisplayMode.FACE_AND_LINE;
        this.viewer.scene.globe.HypsometricSetting = {
          hypsometricSetting : this.hyp,
          analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
        }
      }
        break;
      default : break;
    }
  }

  //等值距
  changeEquivalent(value){
    this.hyp.LineInterval = parseFloat(value);
    this.viewer.scene.globe.HypsometricSetting = {
      hypsometricSetting : this.hyp,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    }
  }

  //最大可见高程
  changeFillmaxHeight(value){
    hyp.MaxVisibleValue = parseFloat(value);
    this.viewer.scene.globe.HypsometricSetting = {
      hypsometricSetting : this.hyp,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    };
  }

  //最小可见高程
  changeFillminHeight(){
    this.hyp.MinVisibleValue = parseFloat(value);
    this.viewer.scene.globe.HypsometricSetting = {
      hypsometricSetting : hyp,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    }
  }

  //等值线的颜色
  changeColor(value){
    this.hyp._lineColor = Cesium.Color.fromCssColorString(value);
    this.viewer.scene.globe.HypsometricSetting = {
      hypsometricSetting : this.hyp,
      analysisMode : Cesium.HypsometricSettingEnum.AnalysisRegionMode.ARM_REGION
    }
  }
}

export {isolineAnalysis}

